package cn.stylefeng.guns.modular.tool.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.justKcApi;
import cn.stylefeng.guns.core.util.justSpCzRzApi;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicGoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.tool.entity.ToolStorageStock;
import cn.stylefeng.guns.modular.tool.model.params.ToolBtnShiftParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolStorageStockParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolStorageStockResult;
import cn.stylefeng.guns.modular.tool.service.ToolBtnShiftService;
import cn.stylefeng.guns.modular.tool.service.ToolStorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 云仓库存表控制器
 *
 * @author zhucx
 * @Date 2023-04-01 11:07:07
 */
@Controller
@RequestMapping("/toolStorageStock")
public class ToolStorageStockController extends BaseController {

    private String PREFIX = "/tool/toolStorageStock";

    @Autowired
    private ToolStorageStockService toolStorageStockService;
    @Autowired
    private BasicOperateService basicOperateService;
    @Autowired
    private BasicCustomerService basicCustomerService;
    @Autowired
    private BasicWarehouseService basicWarehouseService;
    @Autowired
    private BasicAreaService basicAreaService;
    @Autowired
    private BasicPositionService basicPositionService;
    @Autowired
    private BasicGoodsService basicGoodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private ToolBtnShiftService toolBtnShiftService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2023-04-01
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolStorageStock.html";
    }
    
    /**
     * 批量删除接口
     */
    @RequestMapping("/btnGroupRevoke")
    @ResponseBody
    public ResponseData btnGroupRevoke(Integer[] ids) {
    	for (Integer id : ids) {
    		ToolStorageStockParam storageStockParam = new ToolStorageStockParam();
    		storageStockParam.setId(id);
			this.toolStorageStockService.delete(storageStockParam);
		}
        return ResponseData.success();
    }
    
    /**
     * 批量移库
     * 选择目的地库位  页面
     */
    @RequestMapping("tobtnShift")
    public String tobtnShift(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/btnShift.html";
    }
    
    /**
     * 批量移库
     */
    @RequestMapping("/btnShift")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData btnShift(ToolStorageStockParam toolStorageStockParam, Long[] ids, String[] shiftNums) {
    	User user = userService.getcurrentUser();
    	if(toolStorageStockParam.getPositionId() == null) {
    		BasicPositionParam basicPositionParam = new BasicPositionParam();
    		basicPositionParam.setWarehouseId(Integer.parseInt(toolStorageStockParam.getWarehouseId().toString()));
    		basicPositionParam.setPositionAreaId(Integer.parseInt(toolStorageStockParam.getAreaId().toString()));
    		basicPositionParam.setPositionNumber(toolStorageStockParam.getPositionNumber());
    		BasicPositionResult basicPositionResult = basicPositionService.findBySpec(basicPositionParam, user.getDeptId(), user.getOperateId());
    		if(basicPositionResult == null) {
    			return ResponseData.error(toolStorageStockParam.getPositionNumber()+"库位不存在");
    		}
    		toolStorageStockParam.setPositionId(Long.parseLong(basicPositionResult.getId().toString()));
    	}
    	List<ToolStorageStockParam> _list = new ArrayList<ToolStorageStockParam>();
    	for (int i = 0; i < ids.length; i++) {
    		ToolStorageStockParam o = new ToolStorageStockParam();
    		o.setId(Integer.parseInt(ids[i].toString()));
    		o.setShiftNum(shiftNums[i]);
    		_list.add(o);
		}
        BasicWarehouse basicWarehouse = basicWarehouseService.getById(toolStorageStockParam.getWarehouseId());
        BasicArea basicArea  = basicAreaService.getById(toolStorageStockParam.getAreaId());
        BasicPosition basicPosition = basicPositionService.getById(toolStorageStockParam.getPositionId());
		List<ToolStorageStockParam> storageList = new ArrayList<ToolStorageStockParam>();
		List<ToolBtnShiftParam> btnShiftList = new ArrayList<ToolBtnShiftParam>();
		BigDecimal as = new BigDecimal("0");
    	for (ToolStorageStockParam p : _list) {
    		ToolStorageStock storageStock = toolStorageStockService.getById(p.getId());
    		BigDecimal kcshu = new BigDecimal(storageStock.getNum());//库存数
    		BigDecimal ykshu = new BigDecimal(p.getShiftNum());//移库数
    		if(storageStock.getPositionId().equals(toolStorageStockParam.getPositionId())) {
    			continue;
    		}
    		if(kcshu.compareTo(as) != 1) {
    			continue;
    		}
    		if(ykshu.compareTo(as) != 1) {
    			continue;
    		}
    		if(ykshu.compareTo(kcshu) == 1) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
				return ResponseData.error("不可大于库存数");
    		}
    		ToolBtnShiftParam btnShiftParam = new ToolBtnShiftParam();
    		btnShiftParam.setWarehouseId(toolStorageStockParam.getWarehouseId());
    		btnShiftParam.setWarehouseName(basicWarehouse.getWarehouseName());
    		btnShiftParam.setYpositionId(storageStock.getPositionId());
    		btnShiftParam.setYpositionNumber(storageStock.getPositionNumber());
    		btnShiftParam.setMpositionId(toolStorageStockParam.getPositionId());
    		btnShiftParam.setMpositionNumber(basicPosition.getPositionNumber());
    		btnShiftParam.setGoodsId(storageStock.getGoodsId());
    		btnShiftParam.setGoodsName(storageStock.getGoodsName());
    		btnShiftParam.setGoodsNumber(storageStock.getGoodsNumber());
    		btnShiftParam.setNum(ykshu.toString());
    		btnShiftParam.setDeptId(user.getDeptId());
    		btnShiftParam.setDeliverId(user.getDeliverId());
    		btnShiftParam.setCreateTime(new Date());
    		btnShiftParam.setCreateName(user.getName());
    		btnShiftParam.setCreateAccount(user.getAccount());
    		btnShiftList.add(btnShiftParam);

    		if(ykshu.compareTo(kcshu) == 0) {//整托移库  有相同属性库存进行合并
    			ToolStorageStockParam xiangtongParam = new ToolStorageStockParam();
    			xiangtongParam.setPositionId(toolStorageStockParam.getPositionId());
    			xiangtongParam.setGoodsId(storageStock.getGoodsId());
    			ToolStorageStockResult xiangtongr = toolStorageStockService.findBySpec(xiangtongParam);
    			if(xiangtongr != null) {
					BigDecimal xtnum = new BigDecimal(xiangtongr.getNum());
        			BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
        			ToolStorageStockParam supdate = new ToolStorageStockParam();
        			ToolUtil.copyProperties(xiangtongr, supdate);
        			BigDecimal xshu = kcshu.add(xtnum).setScale(2, BigDecimal.ROUND_HALF_DOWN);//相加
        			supdate.setNum(xshu.toString());
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
    					BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
    					supdate.setStockWeight(w.multiply(xshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
    				} else {
    					supdate.setStockWeight("1");//重量
    				}
    				
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
    					BigDecimal gv = new BigDecimal(basicGoods.getGoodsVolume());
    					supdate.setStockVolume(gv.multiply(xshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
    				} else {
    					supdate.setStockVolume("1");//体积
    				}
    				
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
    					BigDecimal price = new BigDecimal(basicGoods.getGoodsCharging());
    					supdate.setStockMoney(price.multiply(xshu).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
    					supdate.setUnitprice(basicGoods.getGoodsCharging());//单价
    				} else {
    					supdate.setUnitprice("1");//单价
    					supdate.setStockMoney(xshu.toString());//库存金额
    				}
    				toolStorageStockService.update(supdate);//更新相同库存
    				ToolStorageStockParam depdate = new ToolStorageStockParam();
    				depdate.setId(storageStock.getId());
    				toolStorageStockService.delete(depdate);
    			} else {
    				ToolStorageStockParam supdate = new ToolStorageStockParam();
    				ToolUtil.copyProperties(storageStock, supdate);
    				supdate.setWarehouseId(toolStorageStockParam.getWarehouseId());
    				supdate.setWarehouseName(basicWarehouse.getWarehouseName());
    				supdate.setAreaId(toolStorageStockParam.getAreaId());
    				supdate.setAreaNumber(basicArea.getAreaNumber());
    				supdate.setPositionId(toolStorageStockParam.getPositionId());
    				supdate.setPositionNumber(basicPosition.getPositionNumber());
    				toolStorageStockService.update(supdate);
    			}
    		}
    		if(ykshu.compareTo(kcshu) == -1) {//移出部分	有相同属性库存进行合并
    			BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
    			ToolStorageStockParam supdate = new ToolStorageStockParam();
    			ToolUtil.copyProperties(storageStock, supdate);
    			BigDecimal sykcshu = kcshu.subtract(ykshu).setScale(2, BigDecimal.ROUND_HALF_DOWN);//剩余库存
    			supdate.setNum(sykcshu.toString());
				if (ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
					BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
					supdate.setStockWeight(w.multiply(sykcshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
				} else {
					supdate.setStockWeight("1");//重量
				}
				
				if (ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
					BigDecimal gv = new BigDecimal(basicGoods.getGoodsVolume());
					supdate.setStockVolume(gv.multiply(sykcshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
				} else {
					supdate.setStockVolume("1");//体积
				}
				
				if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
					BigDecimal price = new BigDecimal(basicGoods.getGoodsCharging());
					supdate.setStockMoney(price.multiply(sykcshu).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
					supdate.setUnitprice(basicGoods.getGoodsCharging());//单价
				} else {
					supdate.setUnitprice("1");//单价
					supdate.setStockMoney(sykcshu.toString());//库存金额
				}
				toolStorageStockService.update(supdate);//更新剩余库存

				ToolStorageStockParam xiangtongParam = new ToolStorageStockParam();
    			xiangtongParam.setPositionId(toolStorageStockParam.getPositionId());
    			xiangtongParam.setGoodsId(storageStock.getGoodsId());
    			ToolStorageStockResult xiangtongr = toolStorageStockService.findBySpec(xiangtongParam);
    			if(xiangtongr != null) {
					BigDecimal xtnum = new BigDecimal(xiangtongr.getNum());
					ToolStorageStockParam xtupdate = new ToolStorageStockParam();
        			ToolUtil.copyProperties(xiangtongr, xtupdate);
        			BigDecimal xshu = ykshu.add(xtnum).setScale(2, BigDecimal.ROUND_HALF_DOWN);//相加
        			xtupdate.setNum(xshu.toString());
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
    					BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
    					xtupdate.setStockWeight(w.multiply(xshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
    				} else {
    					xtupdate.setStockWeight("1");//重量
    				}
    				
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
    					BigDecimal gv = new BigDecimal(basicGoods.getGoodsVolume());
    					xtupdate.setStockVolume(gv.multiply(xshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
    				} else {
    					xtupdate.setStockVolume("1");//体积
    				}
    				
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
    					BigDecimal price = new BigDecimal(basicGoods.getGoodsCharging());
    					xtupdate.setStockMoney(price.multiply(xshu).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
    					xtupdate.setUnitprice(basicGoods.getGoodsCharging());//单价
    				} else {
    					xtupdate.setUnitprice("1");//单价
    					xtupdate.setStockMoney(xshu.toString());//库存金额
    				}
    				toolStorageStockService.update(xtupdate);//更新相同库存
    			} else {
    				ToolStorageStockParam sadd = new ToolStorageStockParam();
    				ToolUtil.copyProperties(storageStock, sadd);
    				sadd.setId(null);
    				sadd.setCreateTime(new Date());
    				sadd.setWarehouseId(toolStorageStockParam.getWarehouseId());
    				sadd.setWarehouseName(basicWarehouse.getWarehouseName());
    				sadd.setAreaId(toolStorageStockParam.getAreaId());
    				sadd.setAreaNumber(basicArea.getAreaNumber());
    				sadd.setPositionId(toolStorageStockParam.getPositionId());
    				sadd.setPositionNumber(basicPosition.getPositionNumber());
    				sadd.setNum(ykshu.toString());
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
    					BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
    					sadd.setStockWeight(w.multiply(ykshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
    				} else {
    					sadd.setStockWeight("1");//重量
    				}
    				
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
    					BigDecimal gv = new BigDecimal(basicGoods.getGoodsVolume());
    					sadd.setStockVolume(gv.multiply(ykshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
    				} else {
    					sadd.setStockVolume("1");//体积
    				}
    				
    				if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
    					BigDecimal price = new BigDecimal(basicGoods.getGoodsCharging());
    					sadd.setStockMoney(price.multiply(ykshu).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
    					sadd.setUnitprice(basicGoods.getGoodsCharging());//单价
    				} else {
    					sadd.setUnitprice("1");//单价
    					sadd.setStockMoney(ykshu.toString());//库存金额
    				}
    				storageList.add(sadd);
    			}
    		}
		}
    	if(storageList.size() > 0) {
			toolStorageStockService.batchAddList(storageList);
    	}
    	if (btnShiftList.size() > 0) {
    		toolBtnShiftService.batchAddList(btnShiftList);
		}
		return ResponseData.success();
    }
    
    /**
     * 拉取聚水潭拣货暂存位调入明细  页面
     */
    @RequestMapping("topulljstzcPutDetailed")
    public String topulljstzcPutDetailed(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/pulljstzcPutDetailed.html";
    }

    /**
     * 拉取聚水潭拣货暂存位调入明细
     * @throws InterruptedException 
     * @throws ParseException 
     * @throws IOException 
     * @throws NumberFormatException 
     */
    @RequestMapping("/pulljstzcPutDetailed")
    @ResponseBody
    public ResponseData pulljstzcPutDetailed(ToolStorageStockParam toolStorageStockParam,String putTime) throws InterruptedException, ParseException, NumberFormatException, IOException  {
    	User user = userService.getcurrentUser();
    	ResponseData resdata = new ResponseData();
    	resdata.setSuccess(true);
    	BasicCustomer basicCustomer = basicCustomerService.getById(toolStorageStockParam.getCustomerId());
    	if(ToolUtil.isEmpty(basicCustomer.getJustAppkey())) {
    		return ResponseData.error("请添加基础信息客户聚水潭appKey");
    	} 
    	if(ToolUtil.isEmpty(basicCustomer.getJustAppsecret())) {
    		return ResponseData.error("请添加基础信息客户聚水潭appSecret");
    	} 
    	if(ToolUtil.isEmpty(basicCustomer.getAccessTokenme())) {
    		return ResponseData.error("请添加基础信息客户聚水潭我方授权码");
    	} 
    	if(ToolUtil.isEmpty(basicCustomer.getOwnercoId())) {
    		return ResponseData.error("请添加基础信息客户聚水潭货主编码");
    	} 
		List<ToolStorageStockParam> storageList = new ArrayList<ToolStorageStockParam>();
		BigDecimal as = new BigDecimal("0");
    	List<String> types = new ArrayList<>();
    	types.add("'Move'");//移货
    	types.add("'MoveSku'");//移货
    	types.add("'pick'");//拣货
        BasicOperate basicOperate = basicOperateService.getById(toolStorageStockParam.getOperateId());
        BasicWarehouse basicWarehouse = basicWarehouseService.getById(toolStorageStockParam.getWarehouseId());
        BasicArea basicArea  = basicAreaService.getById(toolStorageStockParam.getAreaId());
		BasicPositionParam basicPositionParam = new BasicPositionParam();
		basicPositionParam.setWarehouseId(Integer.parseInt(toolStorageStockParam.getWarehouseId().toString()));
		basicPositionParam.setPositionAreaId(Integer.parseInt(toolStorageStockParam.getAreaId().toString()));
		basicPositionParam.setPositionNumber(toolStorageStockParam.getPositionNumber());
		BasicPositionResult basicPositionResult = basicPositionService.findBySpec(basicPositionParam, user.getDeptId(), user.getOperateId());
		if(basicPositionResult == null) {
			return ResponseData.error(toolStorageStockParam.getPositionNumber()+"库位不存在");
		}
		toolStorageStockParam.setPositionId(Long.parseLong(basicPositionResult.getId().toString()));
        BasicPosition basicPosition = basicPositionService.getById(toolStorageStockParam.getPositionId());
		String data = justSpCzRzApi.getSellerItem(basicCustomer.getJustAppkey(),basicCustomer.getJustAppsecret(),basicCustomer.getAccessTokenme(), types,Integer.parseInt(basicCustomer.getOwnercoId()),putTime+" 00:00:00",putTime+" 23:59:59");
		JSONObject jsonObject = new JSONObject(data);
		if(!jsonObject.get("code").toString().equals("0")) {
    		return ResponseData.error(jsonObject.get("msg").toString());
		}
		JSONArray jsonArray = new JSONArray(jsonObject.get("data").toString());
		for (Object object : jsonArray) {
			JSONObject jsonObject2 = new JSONObject(object.toString());

			String sku_id = jsonObject2.get("sku_id").toString();//商品编码
			if(sku_id.equals("goodcard5")) {
				continue;
			}
			
			String autoid = jsonObject2.get("autoid").toString();//序号
			ToolStorageStockParam checkParam = new ToolStorageStockParam();
			checkParam.setMark(autoid);
			checkParam.setWarehouseId(toolStorageStockParam.getWarehouseId());
			ToolStorageStockResult check = toolStorageStockService.findBySpec(checkParam);
			if(check != null) {
				continue;//通过序号  去重
			}
			
			ToolStorageStockParam sParam = new ToolStorageStockParam();
			sParam.setMark(autoid);

			String type = jsonObject2.get("type").toString();//类型
			if(type.equals("Move") || type.equals("MoveSku")) {
				String id2 = jsonObject2.get("id2").toString();//目的地
				if(!id2.equals("912168425000001003")) {
					continue;//目的地箱号等于 拣货暂存位
				}
				
				String id1 = jsonObject2.get("id1").toString();//源
				if(id1.equals("912168425000001003")) {
					continue;//源箱号等于 拣货暂存位
				}
			}
			
			sParam.setOperateId(toolStorageStockParam.getOperateId());
			sParam.setOperateName(basicOperate.getOperateName());//分公司名
			sParam.setCustomerId(toolStorageStockParam.getCustomerId());
			sParam.setCustomerName(basicCustomer.getCustomerName());
			sParam.setWarehouseId(toolStorageStockParam.getWarehouseId());
			sParam.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名
			sParam.setAreaId(toolStorageStockParam.getAreaId());
			sParam.setAreaNumber(basicArea.getAreaNumber());//库区号
			sParam.setPositionId(toolStorageStockParam.getPositionId());
			sParam.setPositionNumber(basicPosition.getPositionNumber());//库位
			
	    	BasicGoodsParam bgpParam = new BasicGoodsParam();
	    	bgpParam.setGoodsNumber(sku_id);
	    	bgpParam.setGoodsCustomerId(Integer.parseInt(toolStorageStockParam.getCustomerId().toString()));
	    	BasicGoodsResult grr = basicGoodsService.findBySpec(bgpParam, null);
			//如果商品不存在  新增基础信息
			if(grr==null){
				bgpParam.setGoodsCustomerName(basicCustomer.getCustomerName());//客户名称
				bgpParam.setGoodsName(sku_id);//商品名称
				bgpParam.setGoodsWeight("0.1");//重量单位 默认1
				bgpParam.setGoodsVolume("0.1");//默认1
				bgpParam.setGoodsCharging("10");//默认1
				bgpParam.setGoodsBottom("10");//底
				bgpParam.setGoodsLamination("10");//叠层
				bgpParam.setGoodsTraylayer("1");//托层 默认1
				bgpParam.setGoodsWarranty("36");
				bgpParam.setGoodsSupportvolume("10000");//每托存量 默认100
				bgpParam.setDeliverId(user.getDeliverId());
				bgpParam.setDeptId(user.getDeptId());
				bgpParam.setCreateTime(new Date());
				basicGoodsService.addReturnId(bgpParam);
				grr = new BasicGoodsResult();
				ToolUtil.copyProperties(bgpParam, grr);
			} 
	    	sParam.setGoodsId(Long.parseLong(grr.getId().toString()));
	    	sParam.setGoodsNumber(sku_id);
	    	sParam.setGoodsName(grr.getGoodsName());
	    	
			String qty = jsonObject2.get("qty").toString();//数量
			BigDecimal num = new BigDecimal(qty);
			if(num.compareTo(as) != 1) {
				continue;//库存数大于0
			}
	    	
	    	sParam.setNum(qty);
	        
			if (ToolUtil.isNotEmpty(grr.getGoodsWeight())) {
				BigDecimal w = new BigDecimal(grr.getGoodsWeight());
				sParam.setStockWeight(w.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
			} else {
				sParam.setStockWeight("1");//重量
			}
			
			if (ToolUtil.isNotEmpty(grr.getGoodsVolume())) {
				BigDecimal gv = new BigDecimal(grr.getGoodsVolume());
				sParam.setStockVolume(gv.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
			} else {
				sParam.setStockVolume("1");//体积
			}
			
			if (ToolUtil.isNotEmpty(grr.getGoodsCharging())) {
				BigDecimal p = new BigDecimal(grr.getGoodsCharging());
				sParam.setStockMoney(p.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
				sParam.setUnitprice(grr.getGoodsCharging());//单价
			} else {
				sParam.setUnitprice("1");//单价
				sParam.setStockMoney(qty);//库存金额
			}
			sParam.setDeptId(user.getDeptId());
			sParam.setDeliverId(user.getDeliverId());
			sParam.setCreateTime(new Date());
			sParam.setStockstate("在库");
	        storageList.add(sParam);
		}
    	if(storageList.size() > 0) {
			toolStorageStockService.batchAddList(storageList);
    	}
		return resdata; 
    }
    
    /**
     * 拉取聚水潭拣货暂存位库存  页面
     */
    @RequestMapping("topulljststorageStock")
    public String topulljststorageStock(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/pulljststorageStock.html";
    }

    /**
     * 拉取聚水潭库存
     * @throws InterruptedException 
     * @throws ParseException 
     * @throws IOException 
     * @throws NumberFormatException 
     */
    @RequestMapping("/pulljststorageStock")
    @ResponseBody
    public ResponseData pulljststorageStock(ToolStorageStockParam toolStorageStockParam,String qingkong) throws InterruptedException, ParseException, NumberFormatException, IOException  {
    	User user = userService.getcurrentUser();
    	BasicCustomer basicCustomer = basicCustomerService.getById(toolStorageStockParam.getCustomerId());
    	if(ToolUtil.isEmpty(basicCustomer.getJustAppkey())) {
    		return ResponseData.error("请添加基础信息客户聚水潭appKey");
    	} 
    	if(ToolUtil.isEmpty(basicCustomer.getJustAppsecret())) {
    		return ResponseData.error("请添加基础信息客户聚水潭appSecret");
    	} 
    	if(ToolUtil.isEmpty(basicCustomer.getAccessTokenme())) {
    		return ResponseData.error("请添加基础信息客户聚水潭我方授权码");
    	} 
    	if(ToolUtil.isEmpty(basicCustomer.getOwnercoId())) {
    		return ResponseData.error("请添加基础信息客户聚水潭货主编码");
    	} 
    	BasicGoodsParam basicGoodsParam = new BasicGoodsParam();
    	basicGoodsParam.setGoodsCustomerId(basicCustomer.getId());
    	List<BasicGoodsResult> glist = basicGoodsService.findListBySpec(basicGoodsParam, null);
		List<ToolStorageStockParam> storageList = new ArrayList<ToolStorageStockParam>();
		BigDecimal as = new BigDecimal("0");
    	List<String> sku_ids = new ArrayList<>();
        BasicOperate basicOperate = basicOperateService.getById(toolStorageStockParam.getOperateId());
        BasicWarehouse basicWarehouse = basicWarehouseService.getById(toolStorageStockParam.getWarehouseId());
        BasicArea basicArea  = basicAreaService.getById(toolStorageStockParam.getAreaId());
		BasicPositionParam basicPositionParam = new BasicPositionParam();
		basicPositionParam.setWarehouseId(Integer.parseInt(toolStorageStockParam.getWarehouseId().toString()));
		basicPositionParam.setPositionAreaId(Integer.parseInt(toolStorageStockParam.getAreaId().toString()));
		basicPositionParam.setPositionNumber(toolStorageStockParam.getPositionNumber());
		BasicPositionResult basicPositionResult = basicPositionService.findBySpec(basicPositionParam, user.getDeptId(), user.getOperateId());
		if(basicPositionResult == null) {
			return ResponseData.error(toolStorageStockParam.getPositionNumber()+"库位不存在");
		}
		toolStorageStockParam.setPositionId(Long.parseLong(basicPositionResult.getId().toString()));
        BasicPosition basicPosition = basicPositionService.getById(toolStorageStockParam.getPositionId());
    	for (BasicGoodsResult gr : glist) {
    		sku_ids.add("'"+gr.getGoodsNumber()+"'");
    		if(sku_ids.size() == 20) {//20个品获取一次
				String data = justKcApi.getSellerItem(basicCustomer.getJustAppkey(),basicCustomer.getJustAppsecret(),basicCustomer.getAccessTokenme(), sku_ids,"DefaultPack",Integer.parseInt(basicCustomer.getOwnercoId().replaceAll("-", "")));
				//String data = justKcApi.getSellerItemTest(sku_ids,"DefaultPack");
				JSONObject jsonObject = new JSONObject(data);
				if(!jsonObject.get("code").toString().equals("0")&&!jsonObject.get("code").toString().equals("null")) {
		    		return ResponseData.error(jsonObject.get("msg").toString());
				}
				JSONObject jsonObject1 = new JSONObject(jsonObject.get("data").toString());
				if(Integer.parseInt(jsonObject1.get("date_count").toString()) > 0) {
					JSONArray jsonArray = new JSONArray(jsonObject1.get("datas").toString());
					for (Object object : jsonArray) {
						JSONObject jsonObject2 = new JSONObject(object.toString());
						ToolStorageStockParam sParam = new ToolStorageStockParam();

						String qty = jsonObject2.get("qty").toString();//数量
						BigDecimal num = new BigDecimal(qty);
						if(num.compareTo(as) != 1) {
							continue;//库存数大于0
						}

						String pack_id = jsonObject2.get("pack_id").toString();//箱号
						//次品仓暂存位912168425000004000
						//通用暂存位912168425000001000
						//拣货暂存位910048859000001003
						if(pack_id.equals("910048859000001003")||pack_id.equals("912168425000001000")) {
						} else {
							continue;
						}
						
						sParam.setOperateId(toolStorageStockParam.getOperateId());
						sParam.setOperateName(toolStorageStockParam.getOperateName());//分公司名
						sParam.setCustomerId(toolStorageStockParam.getCustomerId());
						sParam.setCustomerName(basicCustomer.getCustomerName());
						sParam.setWarehouseId(toolStorageStockParam.getWarehouseId());
						sParam.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名
						sParam.setAreaId(toolStorageStockParam.getAreaId());
						sParam.setAreaNumber(basicArea.getAreaNumber());//库区号
						sParam.setPositionId(toolStorageStockParam.getPositionId());
						sParam.setPositionNumber(basicPosition.getPositionNumber());//库位

						String sku_id = jsonObject2.get("sku_id").toString();//商品编码
				    	BasicGoodsParam gParam = new BasicGoodsParam();
				    	gParam.setGoodsNumber(sku_id);
				    	gParam.setGoodsCustomerId(Integer.parseInt(toolStorageStockParam.getCustomerId().toString()));
				    	BasicGoodsResult grr = basicGoodsService.findBySpec(gParam, null);
				    	sParam.setGoodsId(Long.parseLong(grr.getId().toString()));
				    	sParam.setGoodsNumber(sku_id);
				    	sParam.setGoodsName(grr.getGoodsName());
				    	
				    	sParam.setNum(qty);
				        
						if (ToolUtil.isNotEmpty(grr.getGoodsWeight())) {
							BigDecimal w = new BigDecimal(grr.getGoodsWeight());
							sParam.setStockWeight(w.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
						} else {
							sParam.setStockWeight("1");//重量
						}
						
						if (ToolUtil.isNotEmpty(grr.getGoodsVolume())) {
							BigDecimal gv = new BigDecimal(grr.getGoodsVolume());
							sParam.setStockVolume(gv.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
						} else {
							sParam.setStockVolume("1");//体积
						}
						
						if (ToolUtil.isNotEmpty(grr.getGoodsCharging())) {
							BigDecimal p = new BigDecimal(grr.getGoodsCharging());
							sParam.setStockMoney(p.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
							sParam.setUnitprice(grr.getGoodsCharging());//单价
						} else {
							sParam.setUnitprice("1");//单价
							sParam.setStockMoney(qty);//库存金额
						}
						sParam.setDeptId(user.getDeptId());
						sParam.setDeliverId(user.getDeliverId());
						sParam.setCreateTime(new Date());
						sParam.setStockstate("在库");
				        storageList.add(sParam);
					}
				}
				Thread.currentThread().sleep(650);//如果有下一页 等待0.65秒
    			sku_ids = new ArrayList<>();
    		}
		}
    	if(sku_ids.size() > 0) {//余数
			String data = justKcApi.getSellerItem(basicCustomer.getJustAppkey(),basicCustomer.getJustAppsecret(),basicCustomer.getAccessTokenme(), sku_ids,"DefaultPack",Integer.parseInt(basicCustomer.getOwnercoId().replaceAll("-", "")));
			//String data = justKcApi.getSellerItemTest(sku_ids,"DefaultPack");
			JSONObject jsonObject = new JSONObject(data);
			JSONObject jsonObject1 = new JSONObject(jsonObject.get("data").toString());
			if(Integer.parseInt(jsonObject1.get("date_count").toString()) > 0) {
				JSONArray jsonArray = new JSONArray(jsonObject1.get("datas").toString());
				for (Object object : jsonArray) {
					JSONObject jsonObject2 = new JSONObject(object.toString());
					ToolStorageStockParam sParam = new ToolStorageStockParam();

					String qty = jsonObject2.get("qty").toString();//数量
					BigDecimal num = new BigDecimal(qty);
					if(num.compareTo(as) != 1) {
						continue;//库存数大于0
					}

					String pack_id = jsonObject2.get("pack_id").toString();//箱号
					//次品仓暂存位912168425000004000
					//通用暂存位912168425000001000
					//拣货暂存位910048859000001003
					if(pack_id.equals("910048859000001003")||pack_id.equals("912168425000001000")) {
					} else {
						continue;
					}
					
					sParam.setOperateId(toolStorageStockParam.getOperateId());
					sParam.setOperateName(basicOperate.getOperateName());//分公司名
					sParam.setCustomerId(toolStorageStockParam.getCustomerId());
					sParam.setCustomerName(basicCustomer.getCustomerName());
					sParam.setWarehouseId(toolStorageStockParam.getWarehouseId());
					sParam.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名
					sParam.setAreaId(toolStorageStockParam.getAreaId());
					sParam.setAreaNumber(basicArea.getAreaNumber());//库区号
					sParam.setPositionId(toolStorageStockParam.getPositionId());
					sParam.setPositionNumber(basicPosition.getPositionNumber());//库位

					String sku_id = jsonObject2.get("sku_id").toString();//商品编码
			    	BasicGoodsParam gParam = new BasicGoodsParam();
			    	gParam.setGoodsNumber(sku_id);
			    	gParam.setGoodsCustomerId(Integer.parseInt(toolStorageStockParam.getCustomerId().toString()));
			    	BasicGoodsResult grr = basicGoodsService.findBySpec(gParam, null);
			    	sParam.setGoodsId(Long.parseLong(grr.getId().toString()));
			    	sParam.setGoodsNumber(sku_id);
			    	sParam.setGoodsName(grr.getGoodsName());
			    	
			    	sParam.setNum(qty);
			        
					if (ToolUtil.isNotEmpty(grr.getGoodsWeight())) {
						BigDecimal w = new BigDecimal(grr.getGoodsWeight());
						sParam.setStockWeight(w.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
					} else {
						sParam.setStockWeight("1");//重量
					}
					
					if (ToolUtil.isNotEmpty(grr.getGoodsVolume())) {
						BigDecimal gv = new BigDecimal(grr.getGoodsVolume());
						sParam.setStockVolume(gv.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
					} else {
						sParam.setStockVolume("1");//体积
					}
					
					if (ToolUtil.isNotEmpty(grr.getGoodsCharging())) {
						BigDecimal p = new BigDecimal(grr.getGoodsCharging());
						sParam.setStockMoney(p.multiply(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
						sParam.setUnitprice(grr.getGoodsCharging());//单价
					} else {
						sParam.setUnitprice("1");//单价
						sParam.setStockMoney(qty);//库存金额
					}
					sParam.setDeptId(user.getDeptId());
					sParam.setDeliverId(user.getDeliverId());
					sParam.setCreateTime(new Date());
					sParam.setStockstate("在库");
			        storageList.add(sParam);
				}
			}
    	}
    	if(storageList.size() > 0) {
    		if(qingkong.equals("1")) {
    			ToolStorageStockParam deleteStockParam = new ToolStorageStockParam();
    			deleteStockParam.setCustomerId(toolStorageStockParam.getCustomerId());
    			deleteStockParam.setWarehouseId(toolStorageStockParam.getWarehouseId());
    			toolStorageStockService.deleteBySpec(deleteStockParam);
    		}
			toolStorageStockService.batchAddList(storageList);
    	} else {
    		return ResponseData.error("未获取到库存");
    	}
		return ResponseData.success(); 
    }

    /**
     * 导入页面
     * @param model
     * @return
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/import.html";
    }

    /**
     * 下载库存模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "assets/excel/toolStorageStock.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream  outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("库存导入模板" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 期初导入
     * @param file
     * @return
     */
    @RequestMapping(value = "/import" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData addImport(@RequestPart("file") MultipartFile file,ToolStorageStockParam toolStorageStockParam) {
        ResponseData data = new ResponseData();
        String fileName = file.getOriginalFilename();
        if (fileName != null){
            try {
               data = toolStorageStockService.batchImport(fileName, file,toolStorageStockParam,data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    /**
     * 新增页面
     * @author zhucx
     * @Date 2023-04-01
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolStorageStock_add.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-04-01
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolStorageStockParam toolStorageStockParam) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
		BasicGoods bG = basicGoodsService.getById(toolStorageStockParam.getGoodsId() );//商品
		BigDecimal detailedPutnum = new BigDecimal(toolStorageStockParam.getNum());//数量
		BigDecimal w = new BigDecimal(bG.getGoodsWeight());//重量
		BigDecimal v = new BigDecimal(bG.getGoodsVolume());//体积
		BigDecimal u = new BigDecimal(bG.getGoodsCharging());//单价
		BigDecimal as = new BigDecimal(0);
		
		BasicPositionParam basicPositionParam = new BasicPositionParam();
		basicPositionParam.setWarehouseId(Integer.parseInt(toolStorageStockParam.getWarehouseId().toString()));
		basicPositionParam.setPositionAreaId(Integer.parseInt(toolStorageStockParam.getAreaId().toString()));
		basicPositionParam.setPositionNumber(toolStorageStockParam.getPositionNumber());
		BasicPositionResult basicPositionResult = basicPositionService.findBySpec(basicPositionParam, user.getDeptId(), user.getOperateId());
		if(basicPositionResult == null) {
			return ResponseData.error(toolStorageStockParam.getPositionNumber()+"库位不存在");
		}
		toolStorageStockParam.setPositionId(Long.parseLong(basicPositionResult.getId().toString()));
		
		if(detailedPutnum.compareTo(as) == 1) {
			ToolStorageStockParam xiangtongParam = new ToolStorageStockParam();
			xiangtongParam.setPositionId(toolStorageStockParam.getPositionId());
			xiangtongParam.setGoodsId(toolStorageStockParam.getGoodsId());
			ToolStorageStockResult xiangtongr = toolStorageStockService.findBySpec(xiangtongParam);
			if(xiangtongr != null) {
				BigDecimal xtnum = new BigDecimal(xiangtongr.getNum());
    			ToolStorageStockParam supdate = new ToolStorageStockParam();
    			BigDecimal xshu = detailedPutnum.add(xtnum).setScale(3, BigDecimal.ROUND_HALF_DOWN);//相加
    			supdate.setId(xiangtongr.getId());
    			supdate.setNum(xshu.toString());
				supdate.setStockWeight(w.multiply(xshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
				supdate.setStockVolume(v.multiply(xshu).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
				supdate.setStockMoney(u.multiply(xshu).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
				supdate.setUnitprice(bG.getGoodsCharging());//单价
				toolStorageStockService.update(supdate);//更新相同库存
			} else {
		        BasicOperate basicOperate = basicOperateService.getById(toolStorageStockParam.getOperateId());
		        BasicCustomer basicCustomer = basicCustomerService.getById(toolStorageStockParam.getCustomerId());
		        BasicWarehouse basicWarehouse = basicWarehouseService.getById(toolStorageStockParam.getWarehouseId());
		        BasicPosition basicPosition = basicPositionService.getById(toolStorageStockParam.getPositionId());
		        BasicArea basicArea  = basicAreaService.getById(toolStorageStockParam.getAreaId());
				ToolStorageStockParam param = new ToolStorageStockParam();
				param.setOperateId(toolStorageStockParam.getOperateId());
				param.setOperateName(basicOperate.getOperateName());
				param.setCustomerId(toolStorageStockParam.getCustomerId());
				param.setCustomerName(basicCustomer.getCustomerName());
				param.setWarehouseId(toolStorageStockParam.getWarehouseId());
				param.setWarehouseName(basicWarehouse.getWarehouseName());
				param.setAreaId(toolStorageStockParam.getAreaId());
				param.setAreaNumber(basicArea.getAreaNumber());
				param.setPositionId(toolStorageStockParam.getPositionId());
				param.setPositionNumber(basicPosition.getPositionNumber());
				param.setGoodsId(toolStorageStockParam.getGoodsId());
				param.setGoodsNumber(bG.getGoodsNumber());
				param.setGoodsName(bG.getGoodsName());
				param.setStockstate("在库");
				param.setDeptId(deptId);
				param.setDeliverId(deliverId);
				param.setNum(detailedPutnum.toString());
				param.setStockWeight(w.multiply(detailedPutnum).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
				param.setStockVolume(v.multiply(detailedPutnum).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
				param.setUnitprice(bG.getGoodsCharging());
				param.setStockMoney(u.multiply(detailedPutnum).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
				param.setGoodsUnit(bG.getGoodsUnit());
				param.setGoodsSpecs(bG.getGoodsSpecs());
				toolStorageStockService.add(param);
			}
		}
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editGroupsStock")
    @ResponseBody
    public ResponseData editGroupsStock(ToolStorageStockParam toolStorageStockParam) {
    	ToolStorageStock storageStock = toolStorageStockService.getById(toolStorageStockParam.getId());
        
        BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
		if (ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
			BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
			BigDecimal n = new BigDecimal(toolStorageStockParam.getNum());
			toolStorageStockParam.setStockWeight(w.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
		} else {
			toolStorageStockParam.setStockWeight("1");//重量
		}
		
		if (ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
			BigDecimal gv = new BigDecimal(basicGoods.getGoodsVolume());
			BigDecimal n = new BigDecimal(toolStorageStockParam.getNum());
			toolStorageStockParam.setStockVolume(gv.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
		} else {
			toolStorageStockParam.setStockVolume("1");//体积
		}
		
		if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
			BigDecimal p = new BigDecimal(basicGoods.getGoodsCharging());
			BigDecimal n = new BigDecimal(toolStorageStockParam.getNum());
			toolStorageStockParam.setStockMoney(p.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
			toolStorageStockParam.setUnitprice(basicGoods.getGoodsCharging());//单价
		} else {
			toolStorageStockParam.setUnitprice("1");//单价
			toolStorageStockParam.setStockMoney(toolStorageStockParam.getNum());//库存金额
		}
		toolStorageStockService.update(toolStorageStockParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     * @author zhucx
     * @Date 2023-04-01
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolStorageStockParam toolStorageStockParam) {
        this.toolStorageStockService.delete(toolStorageStockParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-04-01
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolStorageStockParam toolStorageStockParam) {
        ToolStorageStock detail = this.toolStorageStockService.getById(toolStorageStockParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-04-01
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolStorageStockParam toolStorageStockParam,String condition ) {
    	User user = userService.getcurrentUser();
    	toolStorageStockParam.setDeptId(user.getDeptId());
        return this.toolStorageStockService.findPageBySpec(toolStorageStockParam,condition,user.getOperateId());
    }

}


